package cn.xuexiyuan.springdatamongodemo.user.service.impl;

import cn.xuexiyuan.springdatamongodemo.user.entity.User;
import cn.xuexiyuan.springdatamongodemo.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author 左龙龙
 * @Date 20-1-14
 * @Version 1.0
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(User user) {
        mongoTemplate.save(user);
    }

    @Override
    public List<User> find(String name) {
        Criteria criteria = Criteria.where("status").is(User.Status.Normal);
        if(StringUtils.isNotBlank(name)){
            criteria.and("name").is(name);
        }

        Query query = new Query(criteria);
        query.fields().exclude("passwd");
        return mongoTemplate.find(query, User.class);
    }
}
